package hut.game;

import hut.game.image.ImageData;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashScreen extends Activity implements Runnable {
	public static DisplayMetrics mDisplayMetrics;
	public static int SCREENWIDTH;
	public static int SCREENHEIGHT;
	public float wp,hp;
	public final int DEFAULT_WIDTH = 500;
	public final int DEFAULT_HEIGHT = 600;
	Thread threadsplash;
	ImageView background;
	boolean active = true;
	long timestart = 2000;
	//
	SoundPool soundpool;
	int soundplay;
	final String FILENAME = "resume.txt";
	private final String DATA = "0\n1\n00020xe\n01020ma\n02020tinh\n03020sy\n04010tuong\n05021sy\n06021tinh\n"
			+"07021ma\n08021xe\n21020phao\n27021phao\n30020chot\n32021chot\n34022chot\n36023chot\n38024chot\n60120chot\n"
			+"62121chot\n64122chot\n66123chot\n68124chot\n71120phao\n77121phao\n90120xe\n91120ma\n92120tinh\n93120sy\n"
			+"94110tuong\n95121sy\n96121tinh\n97121ma\n98121xe\n";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// khai bao khong su dung tieu de
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.splashscreen);
		mDisplayMetrics = new DisplayMetrics();
		SCREENWIDTH = mDisplayMetrics.widthPixels;
        SCREENHEIGHT = mDisplayMetrics.heightPixels;
        wp = (float) SCREENWIDTH / DEFAULT_WIDTH;
		hp = (float) SCREENHEIGHT / DEFAULT_HEIGHT;
		// khai bao cai anh nen logo
		background = (ImageView) findViewById(R.id.logo);
		background.setBackgroundResource(R.drawable.logo);
		// bien nay bang true de noi len ko co hanh dong tac dong len man hinh luc no dang quang cao logo
		active = true;
		// khai bao doi tuong de play 1 file am thanh.
		soundpool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
		soundplay = soundpool.load(this, R.raw.intro, 0);
		File file = new File(FILENAME);
		if (!file.exists()) {
			try {
				FileOutputStream fos = openFileOutput(FILENAME,Context.MODE_WORLD_WRITEABLE);
				fos.write(this.DATA.getBytes());
				fos.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ImageData.loadData(getBaseContext());
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	// ham nay duoc goi khi man hinh dien thoai sang, duoc goi sau ham onResume()
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		// neu hien tai ko co tuyen nao chay thi se khoi tao thread moi de chay qua trinh gioi thieu logo
		if (threadsplash == null) {
			threadsplash = new Thread(this);
			threadsplash.start();
		}
		super.onWindowFocusChanged(hasFocus);
	}
	@Override
	protected void onDestroy() {
		threadsplash = null;
		soundpool.stop(AudioManager.STREAM_MUSIC);
		super.onDestroy();
	}
	// phuong thuc nay duoc goi ngay khi thread chay
	@Override
	public void run() {
        // thuc hien cac lenh trong khoi try
		try {
			while (active && (timestart >= 0)) {
				timestart -= 20;
				soundpool.play(soundplay, 0f, 1f, 0, 0, 1.5f);
				Thread.sleep(100);
			}

		} catch (InterruptedException e) {
		} finally {
			startActivity(new Intent(getApplicationContext(),MainActivity.class));
			finish();
		}
	}

	// ham nay de xu li khi co su kien cham tay vao man hinh,
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			active = false;
		}
		return super.onTouchEvent(event);
	}
}